import json

from api.tests.base_test_case import BaseTestCase
from app.models import MailingAddress


class AddressTest(BaseTestCase):
    def test_add_address(self):
        data = {
            "province": 32,
            "city": 3201,
            "area": 320102,
            "address": "XXXXXXX",
            "addressee": "CCC",
            "phone": "15652415141"
        }
        r = self.client.post('/api/address/add/',
                             data=json.dumps(data),
                             content_type='application/json',
                             accept_media_type='application/json',
                             HTTP_AUTHORIZATION='Token ' + self.access_token)
        self.assertEqual(r.status_code, 200)

    def test_get_address(self):
        r = self.client.get('/api/address/',
                            accept_media_type='application/json',
                            HTTP_AUTHORIZATION='Token ' + self.access_token)
        self.assertEqual(r.status_code, 200)

    def test_update_address(self):
        data = {
            "province": 32,
            "city": 3201,
            "area": 320102,
            "address": "XXXXXXXB",
            "addressee": "CCC",
            "phone": "15652415141"
        }
        r = self.client.put('/api/address/update/1/',
                            data=json.dumps(data),
                            content_type='application/json',
                            accept_media_type='application/json',
                            HTTP_AUTHORIZATION='Token ' + self.access_token)
        self.assertEqual(r.status_code, 200)
        address = MailingAddress.objects.get(id=1)
        self.assertEquals(address.address, data['address'])

    def test_default_address(self):
        r = self.client.post('/api/address/default/1/',
                             accept_media_type='application/json',
                             HTTP_AUTHORIZATION='Token ' + self.access_token)
        self.assertEqual(r.status_code, 200)
